package cn.jwelll.bxcstock.ui.carin.upload;

import android.app.Dialog;
import android.view.View;
import cn.jwelll.bxcstock.base.BaseActivity;
import cn.jwelll.bxcstock.base.BasePresenter;
import cn.jwelll.bxcstock.bean.TrainUploadRequest;
import cn.jwelll.bxcstock.ui.dialog.DialogUtils;
import cn.jwelll.bxcstock.viewmodel.itemmodel.ItemCarInPreviewModel;
import cn.jwelll.bxcstock.viewmodel.itemmodel.UploadDetailModel;
import cn.jwelll.bxcstock.bean.ValidateRequest;
import cn.jwelll.bxcstock.net.ApiMiddleHelper;
import cn.jwelll.bxcstock.net.listener.DataCallBackListener;
import cn.jwelll.bxcstock.util.LogUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.reactivex.Flowable;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CarInUploadPresenter extends BasePresenter<CarInUploadContract.View> implements CarInUploadContract.Presenter {
    BaseActivity activity;
    private Dialog mDialog;
    public CarInUploadPresenter(BaseActivity activity) {
        this.activity = activity;
    }

    @Override
    public void start() {

    }

    @Override
    public void carUpload(String id, String waybillCode, String driverName, String carNum, String qeueuNumber, String loadingPoint, double weight, List<UploadDetailModel> materials) {
        Map<String, Object> biddingMap = new HashMap<>();
        biddingMap.put("waybillKey", id);
        biddingMap.put("waybillCode", waybillCode);
        biddingMap.put("driverName", driverName);
        biddingMap.put("carNum", carNum);
        biddingMap.put("qeueuNumber", qeueuNumber);
        biddingMap.put("loadingPoint", loadingPoint);
        biddingMap.put("weight", weight);
        biddingMap.put("materials", materials);

        Flowable<JsonObject> flowable = apiStrores.carUpload(biddingMap);
        ApiMiddleHelper.getInstance().toNetIo2Main(null, flowable, new DataCallBackListener() {
            @Override
            public void onSuccess(String json) {
                LogUtil.d("carUpload-onSuccess-" + json);
                try {
                    if (getView() != null) {
                        activity.hideProgress();
                        getView().uploadSuccess();
                    }
                } catch (Exception e) {
                    onFailure("数据解析错误");
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("carUpload-onFailure-" + msg);
                if (activity != null) {
                    activity.hideProgress();
                    if (mDialog != null && mDialog.isShowing()) {
                        mDialog.dismiss();
                    }
                    mDialog = new DialogUtils.Builder(activity)
                            .setOutTouch(false)
                            .setTitle("上传失败").
                                    setMessage(msg).
                                    setPositiveButton("确定", v -> {
                                    }).
                                    setOnlyPositive().creat();
                    mDialog.show();
                }
            }
        });
    }

    @Override
    public void trainUpload(TrainUploadRequest request) {
        Map<String, Object> biddingMap = new HashMap<>();
        biddingMap.put("railwayCode", request.getRailwayCode());
        biddingMap.put("relationId", request.getRelationId());
        biddingMap.put("billCode", request.getBillCode());
        biddingMap.put("materials", request.getMaterials());

        Flowable<JsonObject> flowable = apiStrores.trainUpload(biddingMap);
        ApiMiddleHelper.getInstance().toNetIo2Main(null, flowable, new DataCallBackListener() {
            @Override
            public void onSuccess(String json) {
                LogUtil.d("trainUpload-onSuccess-" + json);
                try {
                    if (getView() != null) {
                        activity.hideProgress();
                        getView().uploadSuccess();
                    }
                } catch (Exception e) {
                    onFailure("数据解析错误");
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("trainUpload-onFailure-" + msg);
                if (activity != null) {
                    activity.hideProgress();
                    if (mDialog != null && mDialog.isShowing()) {
                        mDialog.dismiss();
                    }
                    mDialog = new DialogUtils.Builder(activity)
                            .setOutTouch(false).
                                    setMessage(msg).
                                    setTitle("上传失败").
                                    setPositiveButton("确定", v -> {
                                    }).
                                    setOnlyPositive().creat();
                    mDialog.show();
                }
            }
        });
    }

    @Override
    public void validateMaterial(String materialCode, String name, String paiHao, String specification, String waybillKey, List<ItemCarInPreviewModel> targetList) {
        Map<String, Object> biddingMap = new HashMap<>();
        ValidateRequest requst = null;
        requst = new ValidateRequest(materialCode, name, paiHao, Double.valueOf(specification), waybillKey);
        LogUtil.d("validateMaterial--" + requst);
        LogUtil.d("validateMaterial--" + materialCode);
        biddingMap.put("prepare", requst);
        biddingMap.put("targetList", targetList);


        Flowable<JsonObject> flowable = apiStrores.validateMaterial(biddingMap);
        ApiMiddleHelper.getInstance().toNetIo2Main(null, flowable, new DataCallBackListener() {
            @Override
            public void onSuccess(String json) {
                LogUtil.d("validateMaterial-onSuccess-" + json);
                try {
                    if (getView() != null) {
                        activity.hideProgress();
                        getView().validateMaterialSuccess(json.equals("false"));
                    }
                } catch (Exception e) {
                    onFailure("数据解析错误");
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("validateMaterial-onFailure-" + msg);
                if (activity != null) {
                    activity.hideProgress();
                    if (mDialog != null && mDialog.isShowing()) {
                        mDialog.dismiss();
                    }
                    mDialog = new DialogUtils.Builder(activity)
                            .setOutTouch(false).
                                    setMessage(msg).
                                    setTitle(materialCode).
                                    setPositiveButton("确定", v -> {
                                    }).setOnlyPositive().creat();
                    mDialog.show();
                }
            }
        });
    }
}
